package org.langqiao.util.uniqueno;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 */
public class RandomNumberUtil {

    /**
     * 增加位数.
     */
    private static final Integer ADD_PLACES = 6;

    /**
     * 每位最大随机数.
     */
    private static final Integer MAX_RANDOM = 10;

    /**
     * yyyyMMddHHmmss + 4位随机数.
     * @return 20位随机数
     */
    public static String createSerialNo() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(new Date()) + random(ADD_PLACES);
    }

    /**
     * 生成随机数.
     * @param length    长度
     * @return  长度为length的随机数
     */
    private static String random(int length) {
        StringBuffer result = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            result.append(random.nextInt(MAX_RANDOM));
        }
        return result.toString();
    }

    /**
     * 创建业务编号
     * @param busDesc 业务描述
     * @return
     */
    public static String createSerialNo(String busDesc) {
    	StringBuffer result = new StringBuffer();
    	long currentTime = System.currentTimeMillis();
		result.append(busDesc).append("_").append(DateFormatUtils.format(currentTime, "yyyyMMddHHmmssSSS")).append(random(4));
		return result.toString();
    }
    
    public static void main(String[] args) {

		for (int i = 0; i < 1000; i++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					System.out.println(random(ADD_PLACES));
				}
			}).start();
		}

    	
    	
    	/*for(int i=0;i<100;i++){
    		String transId = createSerialNo("create") ;
    		System.out.println("交易id：" + transId);
    	}*/
	}
}
